package com.hotel.controller;


import com.hotel.pojo.User;
import com.hotel.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {


     private final UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("username") String username, @RequestParam("password")  String password, HttpSession session
    ){
        ModelAndView mv = new ModelAndView();
        User user=userService.findUser(username,password);

       if(user!=null){
           if(user.getPassword() .equals(password)&&user.getRole().equals("1")){
               session.setAttribute("userid",user.getId());
               mv.setViewName("index");
           }else {
               mv.setViewName("redirect:/yu/list");
           }
        }else {
           mv.setViewName("error");
       }
       return mv;
    }

    @RequestMapping("/register")
    public String register(User user){
        try {
            user.setRole("0");
            userService.addUser(user);
        }catch (DuplicateKeyException e){
            return "registerError";
        }

        return "registerSuccess";
    }


    @RequestMapping("/updatePwd")
    public String update(User user){
        int i = userService.updatePwd(user);
        if (i>0){
            return "suc_a";
        }else {
            return "pwderro";
        }

    }

    @RequestMapping("/addAdmin")
    public String add(User user){
        try {
            userService.addUser(user);
            // 插入成功后的处理逻辑
            System.out.println("用户添加成功");
        } catch (DuplicateKeyException e) {
            // 处理用户名重复的情况
            System.out.println("用户名已存在，请选择其他用户名");
            // 可以记录日志或返回错误信息给前端
            return "userError";
        } catch (Exception e) {
            // 处理其他可能的异常
            System.out.println("添加用户时发生错误: " + e.getMessage());
            // 可以记录日志或返回错误信息给前端
        }
        return "redirect:/user/list";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        userService.deleteUserById(id);
        return "redirect:/user/list";
    }

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView();
        List<User> userList=userService.queryAllUsers();
        mv.addObject("list",userList);
        mv.setViewName("user_list");
        return mv;
    }

    @RequestMapping("/update1")
    public ModelAndView update1(int id){
        ModelAndView mv = new ModelAndView();
        User user = userService.queryUserById(id);
        mv.addObject("u",user);
        mv.setViewName("user_update");
        return mv;
    }

//    @RequestMapping("/update2")
//    public String update2(User user){
//        userService.updateUserById(user);
//        return ("redirect:/user/list");
//    }
    @RequestMapping("/update2")
    public ModelAndView update2(User user){
        ModelAndView mv = new ModelAndView();
        User dbUser = userService.queryUserById(user.getId());
        user.setUsername(dbUser.getUsername());
        userService.updateUserById(user);
        mv.setViewName("redirect:/user/list");
//        if(dbUser!=null){
//            mv.addObject("error","用户名已存在");
//            mv.setViewName("redirect:/user/list");
//        }else {
//            userService.updateUserById(user);
//            mv.setViewName("redirect:/user/list");
//        }

        return mv;
    }

    @RequestMapping("/find")
    public ModelAndView find(String findByUsername ){
        ModelAndView mv = new ModelAndView();
        User user = userService.queryUserByUsername(findByUsername);
        List<User> userList=new ArrayList<>();
        userList.add(user);
        if (user==null){
            userList=userService.queryAllUsers();
            mv.addObject("error","未查询出结果");
        }
        mv.addObject("list",userList);
        mv.setViewName("user_list");
        return mv;
    }

    @RequestMapping("/pwd_update")
    public ModelAndView pwd(int id){
        ModelAndView mv = new ModelAndView();
        User user = userService.queryUserById(id);
        mv.addObject("u",user);
        mv.setViewName("user_update");
        return mv;
    }
}
